Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve rule to not fail for classes with no tests in their packages #1368

Conversation

krzysztof-owczarek
Copy link
Contributor

@krzysztof-owczarek krzysztof-owczarek commented Oct 23, 2024

This will improve the existing rule to test that test classes reside in the same package as their implementation. In particular the rule:

Should not fail if there are multiple test classes with the same simple name and some of the classes have no tests at all

Issue: #1367

@krzysztof-owczarek krzysztof-owczarek force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from 3012ef7 to 28c2894 Compare October 23, 2024 20:02
Copy link
Member

@hankem hankem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution! 💚
I'd like to take a closer look whether we can simplify the rule, but I already wanted to leave one comment on the test. 😉

@@ -0,0 +1,4 @@
package com.tngtech.archunit.library.testclasses.packages.correct.twoimplementationsonetestdir1;

public class OnlyOneImplementationHaveTestAndItIsMatchingImplPackageTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your test to cover the use case of #1367, this should be

Suggested change
public class OnlyOneImplementationHaveTestAndItIsMatchingImplPackageTest {
class MultipleImplementationsWithOnlyOneTestAndInRightPackageTest {

to make your new test actually fail with the old implementation. 🙂

In addition: I'd drop the AndInRightPackage part from this name: It's a relevant aspect to have classes with the same simple name in different packages, so: Is it right or is it wrong??
How about SimpleNameThatOccursInSeveralPackages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was reneming classes at the end and forgot about that one 🤦 Thanks for this suggestion - applied!

@krzysztof-owczarek krzysztof-owczarek force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from d494fc0 to b87bd2f Compare October 25, 2024 08:47
@krzysztof-owczarek
Copy link
Contributor Author

krzysztof-owczarek commented Oct 25, 2024

Thanks a lot for your contribution! 💚 I'd like to take a closer look whether we can simplify the rule, but I already wanted to leave one comment on the test. 😉

I have a feeling it might be done in with a simpler check. Sorry for not persuing that before I have created this PR, but I am just before my vacation and had a little time to create the change 🌴

I have another, simpler draft of such a condition, but it requires a bit sloppy name creation function:

Map<String, List<JavaClass>> testClassesBySimpleClassName = new HashMap<>();
Map<String, JavaClass> classesByPackageName = new HashMap<>();

@Override
public void init(Collection<JavaClass> allClasses) {
    testClassesBySimpleClassName = allClasses.stream()
        .filter(clazz -> clazz.getName().endsWith(testClassSuffix))
        .collect(groupingBy(JavaClass::getSimpleName));

    classesByPackageName = allClasses.stream()
         .collect(toMap(JavaClass::getFullName, Function.identity()));
}

and then:

possibleTestClasses.stream()
    .map(testClazz -> classesByPackageName.getOrDefault(createImplClassNameFromTestClassName(testClazz), null))
    .noneMatch(Objects::nonNull);

having:

private String createImplClassNameFromTestClassName(JavaClass testClazz) {
    return testClazz.getPackageName() + "." + testClazz.getSimpleName().replace(testClassSuffix, "");
}

I am not super happy with this draft, but the condition is much simpler that way. Maybe it will be a step towords some better solution.

@krzysztof-owczarek
Copy link
Contributor Author

Hey @hankem, I will paste the part of annonynous class here, that has a simpler condition, but is based on name resolving as described before. Give me heads up what do you think about it.

private static ArchCondition<JavaClass> resideInTheSamePackageAsTheirTestClasses(String testClassSuffix) {
        return new ArchCondition<JavaClass>("reside in the same package as their test classes") {
            Map<String, List<JavaClass>> testClassesBySimpleClassName = new HashMap<>();
            Map<String, JavaClass> classesByPackageName = new HashMap<>();

            @Override
            public void init(Collection<JavaClass> allClasses) {
                testClassesBySimpleClassName = allClasses.stream()
                        .filter(clazz -> clazz.getName().endsWith(testClassSuffix))
                        .collect(groupingBy(JavaClass::getSimpleName));

                classesByPackageName = allClasses.stream()
                        .collect(toMap(JavaClass::getFullName, Function.identity()));
            }

            @Override
            public void check(JavaClass implementationClass,
                              ConditionEvents events) {
                String implementationClassName = implementationClass.getSimpleName();
                String implementationClassPackageName = implementationClass.getPackageName();
                String possibleTestClassName = implementationClassName + testClassSuffix;
                List<JavaClass> possibleTestClasses = testClassesBySimpleClassName.getOrDefault(possibleTestClassName,
                        emptyList());

                boolean isTestClassInWrongPackage = !possibleTestClasses.isEmpty()
                        && possibleTestClasses.stream().noneMatch(
                        clazz -> clazz.getPackageName().equals(implementationClassPackageName))
                        && implementationNotFoundInTestClassPackages(possibleTestClasses);

                if (isTestClassInWrongPackage) {
                    possibleTestClasses.forEach(wrongTestClass -> {
                        String message = createMessage(wrongTestClass,
                                String.format("does not reside in same package as implementation class <%s>",
                                        implementationClass.getName()));
                        events.add(violated(wrongTestClass, message));
                    });
                }
            }

            private boolean implementationNotFoundInTestClassPackages(List<JavaClass> possibleTestClasses) {
                return possibleTestClasses.stream()
                        .map(testClazz -> classesByPackageName.getOrDefault(
                                resolveImplClassNameFromTestClassName(testClazz), null)
                        )
                        .noneMatch(Objects::nonNull);
            }

            private String resolveImplClassNameFromTestClassName(JavaClass testClazz) {
                return testClazz.getPackageName() + "." + testClazz.getSimpleName().replace(testClassSuffix, "");
            }

        };
    }

@hankem hankem force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from 2eac3cb to 22a38aa Compare January 26, 2025 12:04
Copy link
Member

@hankem hankem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry that it took me so long to get back to this! 🙄
But now that a new release is coming up, I'd really like to include your contribution...

I have one suggestion to further improve the rule. If you like it, we could squash all changes into one commit.

@krzysztof-owczarek
Copy link
Contributor Author

Hi @hankem 👋 Looks good, thanks. I applied your suggestion :)

@hankem hankem force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from 07d72da to 2e7af6a Compare February 10, 2025 19:58
@hankem
Copy link
Member

hankem commented Feb 10, 2025

Thank you! I've squashed all your commits and rebased them to the main branch to get a nice history.
I think it should be good to go. Thanks again for your contribution! 💚

@hankem hankem merged commit 3495fc2 into TNG:main Feb 10, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants